<?php
/**
 * 节点管理
 *
 * @copyright   Copyright 2011, Richie
 * @author      Richie <richbossman@163.com>
 * @package     SMS
 * @link        http://www.letphp.com
 * 
 * $Id: NodeAction.class.php 53 2011-03-25 14:02:39Z 442767452@qq.com $
 */
class NodeAction extends SystemAction {
	
	function _button_node_add(){
		return array('index');
	}
	
	function _button_node_index(){
		return array('add');
	}
	
	function _button_node_edit(){
		return array('index', 'add');
	}
	
	function _before_add(){
		$this->assign('apps', A('Apps')->apps_list());
		$this->assign('node', $this->node_list());
	}
	
	function _before_edit(){
		$this->assign('apps', A('Apps')->apps_list());
		$this->assign('node', $this->node_list());
	}
	
	function _before_remove(){
		
	}
	
	//列表json
	function lst(){
		$result = array();
		$where = "n.status=1 AND n.parent_id=0";
		$this->datagrid['sort'] = isset($_REQUEST['sort']) ? 'n.'.trim($_REQUEST['sort']) : 'n.sort';
		$result['total'] = M()->table(C('DB_PREFIX').'node AS n')->where($where)->count();
		$result['rows'] = M()->table(C('DB_PREFIX').'node AS n')->field('n.node_id, n.node_name, n.node_title, n.sort, n.status, n.add_time, n.remark, a.app_title')
				->join(C('DB_PREFIX').'apps AS a ON a.app_id=n.app_id')
				->page($this->datagrid['page'])
				->limit($this->datagrid['rows'])
				->order($this->datagrid['sort'].' '.$this->datagrid['order'])
				->where($where)
				->findAll();
		foreach ($result['rows'] as $key=>$row){
			$result['rows'][$key]['add_time'] = date('Y-m-d H:i', $row['add_time']);
		}
		$this->ajaxReturn('', $result);
	}
	
	//顶级节点列表
	function node_list($node_id=0){
		$node = M('node')->where('parent_id=0')->order('sort ASC, node_id DESC')->findAll();
		foreach ($node as $key=>$nod){
			$node[$key]['selected'] = ($nod['node_id'] == $node_id) ? true : false;
		}
		return $node;
	}
}
?>